﻿using System;
using System.Collections.Generic;
using System.Text;

using Microsoft.Xna.Framework;

using Daybreak.Core.SceneGraph;

namespace Daybreak.Core.Rendering
{
    public partial class Geometry
    {
        public class Instance : Drawable.Container
        {
            public Geometry Geom;
            public Matrix TransformMatrix;
            public Transform TransformNode;

            public Instance(Geometry geom)
            {
                Geom = geom;
            }

            public Instance(Geometry geom, Scene.Material.Binding[] bindings)
            {
                Bindings = bindings;
                Geom = geom;
            }

            public override void MergeBounding(ref BoundingBox result)
            {
                if (Geom.Mesh == null)
                    return;

                BoundingBox.CreateMerged(ref result, ref Geom.Mesh.Vertices.BoundingVolume, out result);
                //result.Center = Mesh.BoundingVolume.Center;
                //result.Radius = Mesh.BoundingVolume.Radius;
            }

            public override string ToString()
            {
                if (Geom.Mesh == null)
                    return "Empty Geometry instance";

                return string.Format("<instance_geometry url='#{0}'>  mesh parts:{1}", Geom.Name, Geom.Mesh.PartList.Count);
            }

            public override void CacheNodeTransform(Transform transformNode, ref Matrix transformMatrix)
            {
                TransformMatrix = transformMatrix;
                TransformNode = transformNode;

            }

            public override void GetGeometryData(RenderItem slot, Drawable item)
            {
                item.GetGeometryData(slot, this);
            }

            public override void GetWorldTransforms(List<Matrix> matrices)
            {
                matrices.Add(TransformMatrix);
            }

            public override IMaterial GetMaterial(Drawable item)
            {
                return item.Material;
            }
        }
    }
}
